home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / protocol / standard / network / utf / utf.c < prev   
Text File  |  1993-07-14  |  12KB  |  384 lines

  1.  
  2. /*
  3.  * Copyright (C) 1992, Metis Technology, Inc.
  4.  *
  5.  * @(#)utf.c    1.4    11/2/92
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the Metis Library General Public License as
  9.  * published by Metis Technology, Inc.; either version 2, or (at your
  10.  * option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * Metis Library General Public License for more details.
  16.  *
  17.  * In order to receive a copy of the Metis Library General Public License,
  18.  * write to Metis Technology, Inc., 358 Windsor St, Cambridge, MA 02141, USA.
  19.  * This license may also be retrieved by anonymous FTP from the file
  20.  * "pub/LibraryPublicLicense" on the host METIS.COM [140.186.33.40].
  21.  *
  22.  *
  23.  * Universal Character Set (UCS) Transformation Format (UTF) Utilities
  24.  *
  25.  * This file contains four functions which support conversion to and from
  26.  * DIS10646-1.2:1992(E) UCS-2 and UCS-4 character code representations
  27.  * and the UTF multi-octet format.  The detailed algorithm is specified in
  28.  * ANNEX F of the above standard.
  29.  *
  30.  * The four functions defined below are:
  31.  *
  32.  *   int UCS2ToUTF ( UTFCHAR *  utf_str,
  33.  *             UCS2CHAR *    ucs_str,
  34.  *             int    utf_str_size )
  35.  *
  36.  *   int UTFToUCS2 ( UCS2CHAR * ucs_str,
  37.  *             UTFCHAR *  utf_str,
  38.  *             int        ucs_str_size )
  39.  *
  40.  *   int UCS4ToUTF ( UTFCHAR *  utf_str,
  41.  *             UCS4CHAR *    ucs_str,
  42.  *             int    utf_str_size )
  43.  *
  44.  *   int UTFToUCS4 ( UCS4CHAR * ucs_str,
  45.  *             UTFCHAR *  utf_str,
  46.  *             int        ucs_str_size )
  47.  *
  48.  *
  49.  * For each function, the first argument represents a target buffer whose
  50.  * size is specified by the third argument.  This size is not in bytes
  51.  * but in element units according to the element type; e.g., bytes for
  52.  * UTFCHAR, shorts for UCS2CHAR, and longs for UCS4CHAR (these types are
  53.  * defined below).
  54.  *
  55.  * The source string pointed at by the second argument of each function
  56.  * is transformed and copied into the target buffer.  The number of elements
  57.  * consumed from the source string is returned.  In the case of UTFToUCS2,
  58.  * if an invalid UCS-2 code value would be produced from the UTF string
  59.  * data, i.e., a UTF string encoding a value outside the UCS-2 encoding
  60.  * space, then the transformation is terminated and a -1 value is returned
  61.  * indicating an error.
  62.  *
  63.  * If the target buffer is consumed prior to exhausting the source string,
  64.  * transformation will be terminated.  In every case, except the condition
  65.  * specified above for UTFToUCS2, the target buffer is null-terminated
  66.  * at the termination of transformation.  Therefore, the size of the target
  67.  * buffer should be adequate to store the transformed data plus a null
  68.  * terminator (of the appropriate width, depending on the target type).
  69.  *
  70.  * If the target buffer was not large enough to hold one target element,
  71.  * then none of the source is consumed, and a null terminator is placed
  72.  * at the beginning of the target buffer.  In this case, a 0 value is
  73.  * returned indicating that none of the source was consumed.
  74.  * 
  75.  * Notes:
  76.  *
  77.  * 1. Performance might be marginally improved by unrolling a few loops.
  78.  *
  79.  * 2. Interoperability between the UTF produced by this code has not
  80.  *    been tested against another UTF implementation.
  81.  *
  82.  * 3. Upper bounds on target buffer sizes are as follows:
  83.  *   
  84.  *      UCS2ToUTF    Length ( ucs_str ) * 3    + 1
  85.  *      UTFToUCS2    Length ( utf_str )    + 1
  86.  *
  87.  *      UCS4ToUTF    Length ( ucs_str ) * 5  + 1
  88.  *      UTFToUCS4    Length ( utf_str )    + 1
  89.  *
  90.  *    Length(x) returns the number of elements (not bytes) in x.  The
  91.  *    result is the number of elements that the target buffer should be
  92.  *    able to hold.  Room for the null terminator is included in these bounds.
  93.  *
  94.  * 4. Please report any bugs or suggestions to Glenn Adams, <glenn@metis.com>.
  95.  *
  96.  */
  97.  
  98. typedef unsigned long    CARD32;
  99. typedef unsigned short    CARD16;
  100. typedef unsigned char    CARD8;
  101.  
  102. typedef CARD16        UCS2CHAR;
  103. typedef CARD32        UCS4CHAR;
  104. typedef CARD8        UTFCHAR;
  105.  
  106. #define    UCS_RANGE0    0x00000000
  107. #define    UCS_RANGE1    0x000000A0
  108. #define    UCS_RANGE2    0x00000100
  109. #define    UCS_RANGE3    0x00004016
  110. #define    UCS_RANGE4    0x00038E2E
  111.  
  112. #define    UTF_RANGE0    0x00
  113. #define    UTF_RANGE1    0xA0
  114. #define    UTF_RANGE2    0xA1
  115. #define    UTF_RANGE3    0xF6
  116. #define    UTF_RANGE4    0xFC
  117.  
  118. static UTFCHAR T[] = {
  119.   0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
  120.   0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  121.   0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
  122.   0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
  123.   0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  124.   0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
  125.   0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  126.   0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60,
  127.   0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  128.   0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
  129.   0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  130.   0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0xa0, 0xa1,
  131.   0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
  132.   0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
  133.   0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
  134.   0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1,
  135.   0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
  136.   0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
  137.   0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
  138.   0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1,
  139.   0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  140.   0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
  141.   0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
  142.   0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0x00, 0x01,
  143.   0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  144.   0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
  145.   0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
  146.   0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x7f,
  147.   0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  148.   0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  149.   0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  150.   0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
  151. };
  152.  
  153. static UTFCHAR U[] = {
  154.   0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
  155.   0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
  156.   0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5,
  157.   0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd,
  158.   0xde, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  159.   0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  160.   0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
  161.   0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
  162.   0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
  163.   0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
  164.   0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
  165.   0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
  166.   0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
  167.   0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e,
  168.   0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
  169.   0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0xdf,
  170.   0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  171.   0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  172.   0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  173.   0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
  174.   0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65,
  175.   0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
  176.   0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
  177.   0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d,
  178.   0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
  179.   0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d,
  180.   0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
  181.   0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d,
  182.   0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
  183.   0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
  184.   0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5,
  185.   0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd,
  186. };
  187.  
  188. int
  189.   UCS2ToUTF ( utf_str, ucs_str, utf_str_size )
  190. UTFCHAR *  utf_str;
  191. UCS2CHAR * ucs_str;
  192. int       utf_str_size;
  193. {
  194.   register UCS2CHAR *p;
  195.   register UTFCHAR *sp, *esp;
  196.   CARD32 ucs;
  197.  
  198.   if ( ! utf_str || ! ucs_str || ! utf_str_size )
  199.     return 0;
  200.  
  201.   sp  = utf_str;
  202.   esp = & sp [ utf_str_size - 1 ];
  203.   for ( p = ucs_str; ucs = (CARD32) *p; p++ ) {
  204.     if ( ucs < UCS_RANGE1 ) {
  205.       if ( ( esp - sp ) < 1 )
  206.     break;
  207.       *sp++ = (UTFCHAR) ucs;
  208.     } else if ( ucs < UCS_RANGE2 ) {
  209.       if ( ( esp - sp ) < 2 )
  210.     break;
  211.       *sp++ = (UTFCHAR) 0xA0;
  212.       *sp++ = (UTFCHAR) ucs;
  213.     } else if ( ucs < UCS_RANGE3 ) {
  214.       register CARD32 y = ucs - 0x100;
  215.       register CARD32 d = 0xBE;
  216.       register int i;
  217.       if ( ( esp - sp ) < 2 )
  218.     break;
  219.       *sp++ = (UTFCHAR) ( 0xA1 + y / d );
  220.       for ( i = 0; i < 1; i++ )
  221.     *sp++ = T [ ( y / ( d /= 0xBE ) ) % 0xBE ];
  222.     } else {
  223.       register CARD32 y = ucs - 0x4016;
  224.       register CARD32 d = 0xBE * 0xBE;
  225.       register int i;
  226.       if ( ( esp - sp ) < 3 )
  227.     break;
  228.       *sp++ = (UTFCHAR) ( 0xF6 + y / d );
  229.       for ( i = 0; i < 2; i++ )
  230.     *sp++ = T [ ( y / ( d /= 0xBE ) ) % 0xBE ];
  231.     }
  232.   }
  233.   *sp++ = (UTFCHAR) 0x00;
  234.   return (int) ( p - ucs_str );
  235. }
  236.  
  237.  
  238. int
  239.   UTFToUCS2 ( ucs_str, utf_str, ucs_str_size )
  240. UCS2CHAR * ucs_str;
  241. UTFCHAR *  utf_str;
  242. int       ucs_str_size;
  243. {
  244.   register UCS2CHAR *p, *ep;
  245.   register UTFCHAR *sp, utf;
  246.   CARD32 ucs;
  247.  
  248.   if ( ! ucs_str || ! utf_str || ! ucs_str_size )
  249.     return 0;
  250.  
  251.   p   = ucs_str;
  252.   ep  = & p  [ ucs_str_size - 1 ];
  253.   for ( sp = utf_str; utf = *sp; sp++ ) {
  254.     if ( utf < UTF_RANGE1 )
  255.       ucs = (CARD32) utf;
  256.     else if ( utf < UTF_RANGE2 )
  257.       ucs = (CARD32) *++sp;
  258.     else if ( utf < UTF_RANGE3 )
  259.       ucs = (CARD32) ( utf - 0xA1 ) * 0xBE + U [ *++sp ] + 0x100;
  260.     else if ( utf < UTF_RANGE4 ) {
  261.       register int i;
  262.       ucs = (CARD32) ( utf - 0xF6 );
  263.       for ( i = 0; i < 2; i++ )
  264.     ucs = ucs * 0xBE + U [ *++sp ];
  265.       ucs += 0x4016;
  266.     } else {
  267.       register int i;
  268.       ucs = (CARD32) ( utf - 0xFC );
  269.       for ( i = 0; i < 4; i++ )
  270.     ucs = ucs * 0xBE + U [ *++sp ];
  271.       ucs += 0x38E2E;
  272.     }
  273.     if ( ( ep - p ) < 1 )
  274.       break;
  275.     if ( ucs > 0xffff )
  276.       return -1;
  277.     *p++ = ucs;
  278.   }
  279.   *p++ = (UCS2CHAR) 0x0000;
  280.   return (int) ( sp - utf_str );
  281. }
  282.  
  283. int
  284.   UCS4ToUTF ( utf_str, ucs_str, utf_str_size )
  285. UTFCHAR *  utf_str;
  286. UCS4CHAR * ucs_str;
  287. int       utf_str_size;
  288. {
  289.   register UCS4CHAR *p;
  290.   register UTFCHAR *sp, *esp;
  291.   CARD32 ucs;
  292.  
  293.   if ( ! utf_str || ! ucs_str || ! utf_str_size )
  294.     return 0;
  295.  
  296.   sp  = utf_str;
  297.   esp = & sp [ utf_str_size - 1 ];
  298.   for ( p = ucs_str; ucs = (CARD32) *p; p++ ) {
  299.     if ( ucs < UCS_RANGE1 ) {
  300.       if ( ( esp - sp ) < 1 )
  301.     break;
  302.       *sp++ = (UTFCHAR) ucs;
  303.     } else if ( ucs < UCS_RANGE2 ) {
  304.       if ( ( esp - sp ) < 2 )
  305.     break;
  306.       *sp++ = (UTFCHAR) 0xA0;
  307.       *sp++ = (UTFCHAR) ucs;
  308.     } else if ( ucs < UCS_RANGE3 ) {
  309.       register CARD32 y = ucs - 0x100;
  310.       register CARD32 d = 0xBE;
  311.       register int i;
  312.       if ( ( esp - sp ) < 2 )
  313.     break;
  314.       *sp++ = (UTFCHAR) ( 0xA1 + y / d );
  315.       for ( i = 0; i < 1; i++ )
  316.     *sp++ = T [ ( y / ( d /= 0xBE ) ) % 0xBE ];
  317.     } else if ( ucs < UCS_RANGE4 ) {
  318.       register CARD32 y = ucs - 0x4016;
  319.       register CARD32 d = 0xBE * 0xBE;
  320.       register int i;
  321.       if ( ( esp - sp ) < 3 )
  322.     break;
  323.       *sp++ = (UTFCHAR) ( 0xF6 + y / d );
  324.       for ( i = 0; i < 2; i++ )
  325.     *sp++ = T [ ( y / ( d /= 0xBE ) ) % 0xBE ];
  326.     } else {
  327.       register CARD32 y = ucs - 0x38E2E;
  328.       register CARD32 d = 0xBE * 0xBE * 0xBE * 0xBE;
  329.       register int i;
  330.       if ( ( esp - sp ) < 5 )
  331.     break;
  332.       *sp++ = (UTFCHAR) ( 0xFC + y / d );
  333.       for ( i = 0; i < 4; i++ )
  334.     *sp++ = T [ ( y / ( d /= 0xBE ) ) % 0xBE ];
  335.     }
  336.   }
  337.   *sp++ = (UTFCHAR) 0x00;
  338.   return (int) ( p - ucs_str );
  339. }
  340.  
  341.  
  342. int
  343.   UTFToUCS4 ( ucs_str, utf_str, ucs_str_size )
  344. UCS4CHAR * ucs_str;
  345. UTFCHAR *  utf_str;
  346. int       ucs_str_size;
  347. {
  348.   register UCS4CHAR *p, *ep;
  349.   register UTFCHAR *sp, utf;
  350.   CARD32 ucs;
  351.  
  352.   if ( ! ucs_str || ! utf_str || ! ucs_str_size )
  353.     return 0;
  354.  
  355.   p   = ucs_str;
  356.   ep  = & p  [ ucs_str_size - 1 ];
  357.   for ( sp = utf_str; utf = *sp; sp++ ) {
  358.     if ( utf < UTF_RANGE1 )
  359.       ucs = (CARD32) utf;
  360.     else if ( utf < UTF_RANGE2 )
  361.       ucs = (CARD32) *++sp;
  362.     else if ( utf < UTF_RANGE3 )
  363.       ucs = (CARD32) ( utf - 0xA1 ) * 0xBE + U [ *++sp ] + 0x100;
  364.     else if ( utf < UTF_RANGE4 ) {
  365.       register int i;
  366.       ucs = (CARD32) ( utf - 0xF6 );
  367.       for ( i = 0; i < 2; i++ )
  368.     ucs = ucs * 0xBE + U [ *++sp ];
  369.       ucs += 0x4016;
  370.     } else {
  371.       register int i;
  372.       ucs = (CARD32) ( utf - 0xFC );
  373.       for ( i = 0; i < 4; i++ )
  374.     ucs = ucs * 0xBE + U [ *++sp ];
  375.       ucs += 0x38E2E;
  376.     }
  377.     if ( ( ep - p ) < 1 )
  378.       break;
  379.     *p++ = ucs;
  380.   }
  381.   *p++ = (UCS4CHAR) 0x0000;
  382.   return (int) ( sp - utf_str );
  383. }
  384.